<?php
class Zfarm_ModuleController extends Zend_Controller_Action
{
    public function indexAction(){
        $this->_helper->viewRenderer->setNoRender();
        $form = new Zend_Form;
        $form->setAction('');
        $form->setMethod('post');
        $form->addElement('text', 'moduleName', array('label' => 'Module Name'));
        $form->addElement('submit', 'Create');
        echo $form;
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($_POST)) {
                $values = $form->getValues();
                $this->createFolders($values['moduleName']);
                $this->bootStrapDefaultLayout($values['moduleName']);
            }
        }
    }
    private function createFolders($moduleName){
        $modulePath = APPLICATION_PATH.'/modules/'.$moduleName;
        mkdir($modulePath);
        mkdir($modulePath.'/controllers');
        mkdir($modulePath.'/models');
        mkdir($modulePath.'/models/DbTable');
        mkdir($modulePath.'/views');
    }
    private function bootStrapDefaultLayout($moduleName){
            $modulePath = APPLICATION_PATH.'/modules/'.$moduleName;
            $foo      = new Zend_CodeGenerator_Php_Class();
            $docblock = new Zend_CodeGenerator_Php_Docblock(array(
                'shortDescription' => $moduleName.' bootstrap class',
                'longDescription'  => 'This is a class generated with ZFarm.',
                'tags'             => array(
                    array(
                        'name'        => 'version',
                        'description' => '$Rev:$',
                    ),
                    array(
                        'name'        => 'license',
                        'description' => 'MIT',
                    ),
                ),
            ));
            $foo->setMethods(array(
            // Method passed as array
                array(
                    'name'       => '_initAppAutoload',
                    'body'       => '$autoloader = new Zend_Application_Module_Autoloader(array(' . "\n" . '"namespace" => "'.$moduleName.'",'. "\n" . '"basePath"  => APPLICATION_PATH."/modules/'.$moduleName.'",'. "\n" . '));'. "\n" . '$autoLoader = Zend_Loader_Autoloader::getInstance();'. "\n" . '$autoLoader->registerNamespace("ZFarm_");'. "\n" . 'return $autoloader;',
                ),
                array(
                    'name'       => '_initConfig',
                    'body'       => '$this->config = new Zend_Config($this->getOptions(), true);' . "\n" . 'Zend_Registry::set("config", $this->config);'. "\n" . 'return $this->config;',
                ),
        
            ));
            $foo->setName($moduleName.'_Bootstrap');
            $foo->setExtendedClass('Zend_Application_Module_Bootstrap');
            $foo->setDocblock($docblock);
            
        $foo->generate();
        $file = new Zend_CodeGenerator_Php_File(array('classes'  => array($foo)));
        file_put_contents($modulePath.'/Bootstrap.php', $file);
    }
}